
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

//#define CHANNEL_HALF_NODES 92 
//#define CHANNEL_NODES CHANNEL_HALF_NODES*2

int main()
{
  cout << "For Read .xy file of writing by OpenFoam " << endl;
  //ifstream infile("D:\01_Campus\01_Postgraduate\00_Academia\contentOfWork\JiNanCluster\graphs_temp\3500\T_0_71Mean.xy",ios::in);
 
  ifstream infile("D:\\01_Campus\\01_Postgraduate\\00_Academia\\contentOfWork\\JiNanCluster\\graphs_temp\\3500\\T_0_71Mean.xy", ios::in);

  if(infile.is_open()){
      cout << "successful read the file: " << endl;
  }
  else {
      cout << "Error opening file";
  }
  

  
  double cnt[1111] = {0}; // Initialize the array 
  // double cnt[read_length] = {0}; 

  int i = 0;
  double data = 0;
  int read_length = 0;
  while (infile) {
    infile >> data;
    // FIXME: 
    // TODO: 
    // NOTE:
    // HACK:
    // tttt:  
    cnt[i] = data;
    //All data from .xy file will first store in the array cnt[]
    cout << "read from file: " << i <<" value = " << data << " " << endl;
    i++;
    read_length++; // the number of column muptiple 2
  }

  int column_num = read_length / 2;
  cout << "read_length ::" << column_num << endl;

  cout << "in data :" << cnt[1] <<"   " << cnt[2] << endl;
  vector<double> x;
  vector<double> y;
  ofstream outfile_1;
  outfile_1.open("testttt_line_1.txt");
  for (int j=0; j< read_length;j++) // get the first column, "distance"
  {
    cout << j <<"---"<< cnt[j] << endl;
    x.push_back(cnt[j]);
    j++;
  }

  for (auto &i : x )
  {
      outfile_1 << i << endl;
      //the default path is the present file
  }
;

  cout << "x " << x[1] << endl;
  for (int j = 1; j < read_length; j++) // get the second column, "value"
  {
    cout << j << "---" << cnt[j] << endl;
    y.push_back(cnt[j]);
    j++;
  }
  ofstream outfile_2;
  outfile_2.open("testttt_line_2.txt");
  for (auto& i : y)
  {
      outfile_2 << i << endl;
  }


  outfile_1.close();
  outfile_2.close();

}